const { ccclass, property } = cc._decorator;

@ccclass
export default class ToastHelper extends cc.Component {

    static textsToShow = [];

    static toastShowing = false;

    static show(text: string) {
        if (text.length == 0 || text == null)
            return;
        if (ToastHelper.textsToShow.length >= 2) {
            console.log("Toast: 存量超过2，不显示:" + text)
            return;
        }
        ToastHelper.textsToShow.push(text);
        if (ToastHelper.toastShowing == false) {
            cc.assetManager.getBundle("GameAb").load("prefab/toast", cc.Prefab, (err: Error, pf: cc.Prefab) => {
                if ((err))
                    return console.log(err.message);
                let node = cc.instantiate(pf);
                node.parent = cc.Canvas.instance.node;
                ToastHelper.toastShowing = true;
            })
        }
    }

    start() {
        this.toastOne();
    }

    toastOne() {
        this.node.getChildByName("text").getComponent(cc.Label).string = ToastHelper.textsToShow.shift()
        this.scheduleOnce(this.toastOneDone, 2);
    }

    toastOneDone() {
        this.node.destroy();
        ToastHelper.toastShowing = false;
        if (ToastHelper.textsToShow.length > 0) {
            ToastHelper.show(ToastHelper.textsToShow.shift())
        }
    }
}
